package segger.vasttrafik.parsing;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.log4j.Logger;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class XMLParser {
    private static final Logger logger = Logger.getLogger(XMLParser.class);
    
    public static final void parseFromXMLText(String xmlStr, XMLParseHandler handler) throws ParserConfigurationException, SAXException, IOException {
        logger.info("parsing...");
        SAXParserFactory spf = SAXParserFactory.newInstance();
        InputSource response = new InputSource(new StringReader(xmlStr));
        
        SAXParser sp = spf.newSAXParser();
        sp.parse(response, handler);
    }
    
    public static final void parseFromInputStream(InputStream input, XMLParseHandler handler) throws ParserConfigurationException, SAXException, IOException {
        logger.info("parsing...");
        SAXParserFactory spf = SAXParserFactory.newInstance();
        
        SAXParser sp = spf.newSAXParser();
        sp.parse(input, handler);
    }
}
